package com.qzxy.premiumhotelsystem.controller.guest;

import com.qzxy.premiumhotelsystem.common.annotation.LogExecutionTime;
import com.qzxy.premiumhotelsystem.common.enums.LogLevel;
import com.qzxy.premiumhotelsystem.common.result.Result;
import com.qzxy.premiumhotelsystem.domain.dto.OrderDTO;
import com.qzxy.premiumhotelsystem.domain.dto.OrderRenewDTO;
import com.qzxy.premiumhotelsystem.domain.dto.PayOrderDTO;
import com.qzxy.premiumhotelsystem.service.OrdersService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * ClassName: OrdersController
 * Package: com.qzxy.premiumhotelsystem.controller
 * Description:
 *
 * @Author Qwh
 * @Create 2024/9/12 16:03
 * @Version 1.0
 */

@RestController("userOrdersController")
@RequestMapping("/user/orders")
@Tag(name = "用户预订相关接口")
@Slf4j
public class OrdersController {


    @Resource
    private OrdersService ordersService;


    /**
     * 用户预订生成订单
     * @param orderDTO
     * @return
     */
    @Operation(summary = "用户预订生成订单")
    @PostMapping("/booking")
    public Result createOrder(@RequestBody @Validated OrderDTO orderDTO) {
        log.info("开始生成预订订单 OrderDTO：{}",orderDTO);
        Result result = ordersService.createBookOrder(orderDTO);
        return result;
    }


    @Operation(summary = "用户支付预订订单")
    @PostMapping("/pay")
    public Result payBookingOrder(@ParameterObject @Validated PayOrderDTO payOrderDTO) {
        log.info("开始支付预订订单 PayVipOrderDTO：{}",payOrderDTO);
        Result result = ordersService.payBookOrder(payOrderDTO);
        return result;
    }

    @Operation(summary = "用户订单分页查询")
    @GetMapping("/list")
    public Result getOrders(@RequestParam(required = false,defaultValue = "1") Integer pageNo, @RequestParam(required = false,defaultValue = "15") Integer pageSize){
        log.info("用户端进行订单分页查询");
        Result result = ordersService.pageQueryOrders(pageNo,pageSize);
        return result;
    }

    @Operation(summary = "根据id查询订单详情")
    @GetMapping("/{orderId}")
    public Result getOrderDetail(@PathVariable String orderId){
        log.info("用户端根据orderId订单查询");
        Result result = ordersService.selectOrderDetailVO(orderId);
        return result;
    }


    @Operation(summary = "用户取消预订订单" ,description = "注意订单只有处于未支付或者已支付用户才能取消订单，处于别的状态要取消只能有员工管理员来")
    @PutMapping("/cancel/{orderId}")
    public Result cancelOrder(@PathVariable String orderId){
        log.info("用户取消预订订单 orderId：{}",orderId);
        Result result =  ordersService.cancelOrder(orderId);
        return result;

    }

    @Operation(summary = "用户续订房间",description = "durationDay表示要续订几天")
    @PutMapping("/renew")
    public Result reNewOrder(@ParameterObject @Validated OrderRenewDTO orderRenewDTO){
        log.info("用户开始续订房间 OrderRenewDTO：{}",orderRenewDTO);
        Result result = ordersService.reNewOrder(orderRenewDTO);
        return result;
    }


    @Operation(summary = "删除订单" ,description = "高危操作，test用")
    @LogExecutionTime(message = "删除订单",level = LogLevel.WARN)
    @DeleteMapping("/delete/")
    public Result deleteOrder(@RequestParam String orderId){
        log.info("用户删除预订订单 orderId：{}",orderId);
        return Result.ok(ordersService.removeById(orderId));

    }


}
